import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'my-container.dart';

class Publisher extends StatelessWidget {
  final Map publisher;
  Publisher(
      {this.publisher = const {
        "avatar": "images/thumbnail.jpg",
        "name": "小武",
        "identity": "作者",
        "score": '4.0'
      }});

  Widget left() {
    return Container(
        child: Row(children: [
      CircleAvatar(
          radius: 25.0, backgroundImage: AssetImage(publisher["avatar"])),
      Container(
          margin: EdgeInsets.only(left: 10.0),
          child:
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Text(publisher["name"], style: TextStyle(fontSize: 20.0)),
            Text(publisher["identity"]),
          ]))
    ]));
  }

  Widget right() {
    return Row(children: [
      Container(
          child: Column(children: [
        Text(publisher["score"],
            style: TextStyle(fontWeight: FontWeight.bold, fontSize: 25.0)),
        stars()
      ])),
      Icon(Icons.navigate_next)
    ]);
  }

  Widget starIcon(String icon) {
    Map<String, IconData> icons = {
      "star": Icons.star,
      "border": Icons.star_border,
      "half": Icons.star_half
    };
    return Icon(
      icons[icon],
      color: Color(0xfffa8c16),
      size: 18.0,
    );
  }

  Widget stars() {
    double score = double.parse(publisher["score"]);
    if (score > 6) {
      score = 5.0;
    }
    int solid = score.floor();
    int half = score - solid > 0 ? 1 : 0;
    List<Widget> list = [];
    for (int i = 0; i < solid; i++) {
      list.add(starIcon('star'));
    }
    if (half > 0) list.add(starIcon('half'));
    for (int i = 0; i < 5 - score - half; i++) {
      list.add(starIcon('border'));
    }
    return Row(children: list);
  }

  @override
  Widget build(BuildContext context) {
    return MyContainer(
        margin: [0.0, 0.0, 0.0, 0.0],
        child: Column(children: [
          Row(
              children: [left(), right()],
              mainAxisAlignment: MainAxisAlignment.spaceBetween),
          SizedBox(height: 10.0),
        ]));
  }
}
